subjectNames = { 'c1'  'c2'  'c3'  'c4'  'c5'  'c6'  'c7'  'c8' ...
                 'nd1' 'nd2' 'nd3' 'nd4' 'nd5' 'nd6' 'nd7' 'nd8' };
highpassFilter = 0.5;
lowpassFilter  = 50;

for index = 1:length(subjectNames)
    filenameset = fullfile(subjectNames{index}, [ subjectNames{index} '.set' ]); 
    EEG = pop_loadset(filenameset);

    % filtrage a 0.2
    disp('Filtering data...');
    for cind = 1:EEG.nbchan
       EEG.data(cind,:) = detrend(EEG.data(cind,:));
    end;
    EEG = pop_iirfilt( EEG, highpassFilter, 0, [], [0]);
    EEG = pop_iirfilt( EEG,  0, lowpassFilter, [], [0]);

    % extract all events
    % ------------------
    EEG = pop_epoch( EEG, { 'audio' 'blank' 'both' 'light' }, [-3  3], 'newname', [ EEG.setname ' epochs' ], 'epochinfo', 'yes');
    %EEG = pop_rmbase( EEG, [-200  200]);
    EEG = pop_autorej(EEG, 'nogui', 'on', 'startprob', 5);
    filenameset = fullfile(subjectNames{index}, [ subjectNames{index} '_preprocessed.set' ]);
    EEG = pop_saveset(EEG, filenameset);
end;

% segment datasets
% ----------------
for index = 1:length(subjectNames)
    filenameset = fullfile(subjectNames{index}, [ subjectNames{index} '_preprocessed.set' ]);
    EEG = pop_loadset(filenameset);
    conditions = { 'audio' 'light' 'blank' 'both' };
    %if strcmpi(subjectNames{index}, 's9')), conditions(end) = []; end;
    for c = 1:length(conditions)
        EEGEPO = pop_selectevent( EEG,  'type', { conditions{c} }, 'deleteevents', 'off', 'deleteepochs', 'on', 'invertepochs', 'off');
        EEGEPO.setname = [ EEGEPO.setname ' ' conditions{c} ];
        EEGEPO.condition = conditions{c};
        EEGEPO.group     = fastif(subjectNames{index}(1) == 'c', 'control', 'nondual');
        EEGEPO.subject   = subjectNames{index};
        pop_saveset(EEGEPO, [ filenameset(1:end-4) '_' conditions{c} '.set' ]);
    end;
end;

% create (EEGLAB) STUDY using all subjects
% ----------------------------------------
eeglab;
commands = {};
for index = 1:length(subjectNames);
    filename1  = fullfile(subjectNames{index}, [ subjectNames{index} '_preprocessed_audio.set' ]);
    filename2  = fullfile(subjectNames{index}, [ subjectNames{index} '_preprocessed_light.set' ]);
    filename3  = fullfile(subjectNames{index}, [ subjectNames{index} '_preprocessed_blank.set' ]);
    commands{end+1} = { 'index' 3*index-2 'load' filename1 };
    commands{end+1} = { 'index' 3*index-1 'load' filename2 };
    commands{end+1} = { 'index' 3*index   'load' filename3 };
end;
STUDY = [];
ALLEEG = [];
[STUDY ALLEEG] = std_editset( STUDY, ALLEEG,  'commands', commands);
EEG = ALLEEG;
CURRENTSTUDY = 1;
CURRENTSET = [1:length(ALLEEG)];
eeglab redraw

% copy fields to DATASETINFO
for dat = 1:length(ALLEEG)
    for e = 1:length(ALLEEG(dat).epoch)
        STUDY.datasetinfo(dat).trialinfo(e).presentation = ALLEEG(dat).epoch(e).eventpresentation{1};
        STUDY.datasetinfo(dat).trialinfo(e).session      = num2str(ALLEEG(dat).epoch(e).eventsession{1});
        STUDY.datasetinfo(dat).trialinfo(e).prevevent    = num2str(ALLEEG(dat).epoch(e).eventprevevent{1});
    end;
end;

% build trialinfo for STUDY
% -------------------------
for dat = 1:length(ALLEEG)
    STUDY.datasetinfo(dat).group     = ALLEEG(dat).group;
    STUDY.datasetinfo(dat).condition = ALLEEG(dat).condition;
    for e = 1:length(ALLEEG(dat).epoch)
        STUDY.datasetinfo(dat).trialinfo(e).presentation = ALLEEG(dat).epoch(e).eventpresentation{1};
        STUDY.datasetinfo(dat).trialinfo(e).session      = num2str(ALLEEG(dat).epoch(e).eventsession{1});
    end;
end;

save -mat nondual16subjects.study STUDY
